﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MedicLibrary.Model;
using MedicLibrary.Controler;

namespace MedicLibrary.View
{
    public partial class frmChiTietTuongTac : Form
    {
        public String action;
        NhomThuoc cNT = new NhomThuoc();
        public frmChiTietTuongTac()
        {
            InitializeComponent();
            
        }

        private void frmChiTietNhomThuoc_Load(object sender, EventArgs e)
        {
            if (action == "VIEW" || action == "EDIT") 
            {
                if( cNT.readThongTinNhomThuoc(Session.IDNhomThuoc) == false)
                    MessageBox.Show("Không đọc được thông tin Nhóm Thuốc!");
                else 
                    bindData(); 
            }
               
        }

        private void rtxDacDiem_TextChanged(object sender, EventArgs e)
        {

        }

        public void applyAction(String ac)
        {
            this.action = ac;
            bool status = false;
            if (this.action == "EDIT" || this.action == "ADD")
            {
                status = true;
            }
            else if (this.action == "VIEW")
            {
                status = false;
            }
            txtTenNhom.Enabled = status;
            txtNhomCha.Enabled = status;
            rtxDacDiem.Enabled = status;
            rtxTacDungPhu.Enabled = status;
            rtxGhiChu.Enabled = status;
            if (this.action == "ADD")
                clearForm();
        }

        public void clearForm()
        {
            txtTenNhom.Text = "";
            txtNhomCha.Text = "";
            rtxDacDiem.Text = "";
            rtxTacDungPhu.Text = "";
            rtxGhiChu.Text = "";
        }

        public void bindData()
        {
            txtMaNhom.Text = cNT.MaNhom;
            txtTenNhom.Text = cNT.TenNhom;
            txtNhomCha.Text = cNT.NhomChaID.ToString();
            rtxDacDiem.Text = cNT.DacDiem;
            rtxTacDungPhu.Text = cNT.TacDungPhu;
            rtxGhiChu.Text = cNT.GhiChu;
        }

        public void setData()
        {
            if (action == "ADD")
                this.cNT.getMaxID();
            else if (action == "EDIT")
                this.cNT.ID = Session.IDNhomThuoc;
            this.cNT.MaNhom = txtMaNhom.Text;
            this.cNT.TenNhom = txtTenNhom.Text;
            this.cNT.NhomChaID = Int32.Parse(txtNhomCha.Text);
            this.cNT.DacDiem = rtxDacDiem.Text;
            this.cNT.TacDungPhu = rtxTacDungPhu.Text;
            this.cNT.GhiChu = rtxGhiChu.Text;
            
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            setData();
            if(action == "ADD")
                this.cNT.insertNhomThuoc();
            else if(action == "EDIT")
                this.cNT.updateNhomThuoc();
            this.Close();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            applyAction("EDIT");
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            //delete Nhom Thuoc
        }   
    }
}
